package practice.phase03.module01.mapreduce.playtime;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class PlayTimeReducer extends Reducer<Text, PlayLogBean, Text, PlayLogBean> {
    @Override
    protected void reduce(Text key, Iterable<PlayLogBean> values, Reducer<Text, PlayLogBean, Text, PlayLogBean>.Context context) throws IOException, InterruptedException {
        Long selfDurationAccumulator = 0L;
        Long thirdDurationAccumulator = 0L;
        for (PlayLogBean val : values) {
            selfDurationAccumulator += val.getSelfDuration();
            thirdDurationAccumulator += val.getThirdDuration();
        }
        PlayLogBean playLogBean = new PlayLogBean(key.toString(), selfDurationAccumulator, thirdDurationAccumulator);
        context.write(key, playLogBean);
    }
}
